// pages/nearby/nearby.js
const apiurl = require('../../config').apiurl;
import { Pulibc } from '../../models/public.js'
const pulibc = new Pulibc()
Page({

  onDetailJump(event) {
    console.log(event)
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../nearby-details/nearby-details?id=' + id
    })
  },
  // 搜索
  getSearch(e){
    console.log(e)
    let chain_name = this.data.chain_name
    // console.log()
    if (chain_name.trim()==''){
      this.getLocations()
      return;
    }
    this.getLocations(chain_name)
    this.setData({
      chain_name:""
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    hiddenReAuthorizePop: true,
    nearbyData: [],
    showlist:true,
    locationBool:'' ,  //地理位置授权
    chain_name:''
  },

  // 获取搜索框的值
  getInputSeach(e){
  
    let chain_name = e.detail.value
    console.log(chain_name)
    this.setData({
      chain_name
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   this.getLocations();
  },
  // 获取地理位置
  getLocations(chain_name='') {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        that.setData({
          locationBool:true
        })
        wx.setStorageSync('accuracy', res.accuracy)
        const latitude = res.latitude
        const longitude = res.longitude
        wx.setStorageSync('latitude'),
         wx.setStorageSync('longitude'),
        wx.request({
          url: apiurl + "/chain/index",
          data: {
            lat: latitude,
            lng: longitude,
            chain_name
          },
          success: data => {
            console.log(data)
            let nearbyData = data.data.datas.list
            console.log(nearbyData)
            that.setData({
              nearbyData,
              showlist:true,
            })
          },
          fail: err => {
            // this.setData({
            //   showlist:false,
            // })
            this.getPromission();
          }
        })
      },
      fail(err){
        console.log("err",err)
      that.setData({
        locationBool:false
      })
      that.getPromission();
      }
    })


  },
  getPromission() {
    let that = this;
    console.log("locationBool", that.data.locationBool)
    if (that.data.locationBool == false) {
      wx.showModal({
        title: '用户未授权',
        content: '请开启相应权限哦',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.openSetting({
              success: data => {
                console.log("data", data)
                if (data.authSetting["scope.userLocation"] == true) {
                  that.getLocations();
                } else {
                  that.getPromission();
                }
              }
            })
          }
        }
      })
    }
  },
  /**
   * 重新授权
   */
  reAuthorize() {
    
    this.setData({
      hiddenReAuthorizePop: false
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    }
    if (wx.getStorageSync("accuracy") == false) {
      this.setData({
        showlist: false
      })
      
    }



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})